<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="88px" size="small">
      <el-form-item label="销售特征" prop="saleConfigList">
        <el-select v-model="queryParams.data.saleConfigId" clearable placeholder="请选择销售特征">
          <el-option-group v-for="item in saleConfigList" :key="item.saleName" :label="item.saleName">
            <el-option v-for="option in item.children" :key="option.saleName" :label="option.saleName" :value="option.id">
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button icon="el-icon-plus" plain size="mini" type="primary" @click="handleAdd"></el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button :disabled="multiple" icon="el-icon-delete" plain size="mini" type="danger" @click="handleDelete"></el-button>
      </el-col>
      <right-toolbar export-table-file-name="销售特征产能" export-table="dataTable" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" id="dataTable" :data="brandNameList" @selection-change="handleSelectionChange">
      <el-table-column align="center" label="全选" prop="id" type="selection" width="50"/>
      <el-table-column v-for="(item,index) in  tableHeaderList" :key="index" :label="item.showName" :prop="item.fieldName" :width="item.width" align="center"/>
      <el-table-column v-for="(item,index) in  31" :key="index+30" :label="item+'日'"  width="80" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row["dayMin" + item] }} / {{ scope.row["dayMax" + item] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="140px">
        <template slot-scope="scope">
          <el-button icon="el-icon-delete" size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="800px">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="商品销售特征" prop="saleConfigId">
          <el-select v-model="form.saleConfigId" clearable placeholder="请选择销售特征">
            <el-option-group v-for="item in saleConfigList" :key="item.saleName" :label="item.saleName">
              <el-option v-for="option in item.children" :key="option.saleName" :label="option.saleName" :value="option.id">
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item v-for="(item ,index) in dataConfigList" :key="index" :label="'生产日期:'+(index+1)">
          <el-col :span="3">
            <el-date-picker style="width: 260px"
                            v-model="dataConfigList[index].date" type="daterange" placeholder="选择日期" value-format="yyyy-MM-dd"
                            start-placeholder="开始日期" end-placeholder="结束日期"/>

          </el-col>
          <el-col :span="8" :offset="7">
            <el-col :span="12">
              <el-input v-model="dataConfigList[index].minValue" placeholder="最小产能"/>
            </el-col>
            <el-col :span="12">
              <el-input v-model="dataConfigList[index].maxValue" placeholder="最大产能"/>
            </el-col>
          </el-col>
          <el-col :span="6">
            <el-button icon="el-icon-delete" size="mini" type="danger" @click="deleteDataConfig(index)">删除</el-button>
            <el-button icon="el-icon-plus" size="mini" type="primary" @click="addDataConfigList">添加</el-button>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {add, deleteByIdList, getById, queryPageList} from '@/api/common'

import {getsaleConfigList} from "@/api/aps/goods";
import {getWorkStationList} from "@/api/aps/workStation";
import {getSaleConfigList} from "@/api/aps/saleConfig";
// console.info("xxx: ",uc.urlPrefix)
export default {
  name: "tenantName",
  data() {

    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,

      brandNameList: [],
      workStationList: [],
      saleConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        data: {}
      },
      dataConfigList: [],
      // 表单参数
      form: {
        saleConfigId: undefined,
      },
      // 表单校验
      rules: {},
      tableHeaderList: []
    };
  },
  created() {
    document["pagePath"] = "/apsMakeCapacitySaleConfig";
    // process.env.pagePath = "/tenant"
    this.getList();

    getSaleConfigList({queryPage:false}).then(data => {
      this.saleConfigList = data.data.dataList;
      // console.info("saleConfigList: ", this.saleConfigList);
    });
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      queryPageList(this.queryParams).then(response => {
        response = response.data
        this.tableHeaderList = response.headerList
        this.brandNameList = response.dataList;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();  this.form.id=undefined;
    },
    // 表单重置
    reset() {
      this.form = {
        remark: "",
        tenantCode: "",
        id: undefined,
        tenantName: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.dataConfigList = [];
      this.reset();  this.form.id=undefined;
      this.addDataConfigList();
      this.open = true;
      this.title = "添加商品销售特征产能";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();  this.form.id=undefined;
      let req = {idList: [row.id], pageSize: 1, pageNum: 1};
      getById(req).then(response => {
        this.form = response.data.dataList[0]
        this.open = true;
        this.title = "修改商品销售特征产能";
      });

    },
    /** 提交按钮 */
    submitForm: function () {

      let mqList = [];
      this.dataConfigList.forEach(t => {
        mqList.push({
          beginDate: t.date[0],
          endDate: t.date[1],
          minValue: t.minValue,
          maxValue: t.maxValue
        })
      })
      this.form.makeCapacityConfigList = mqList;
      add(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const idList = row.id ? [row.id] : this.ids
      this.$modal.confirm('是否确认删序号为 <span style="color:red">' + idList + '</span> 的数据项？', "删除提示").then(function () {
        let req = {
          idList: idList
        }
        return deleteByIdList(req);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      });
      document.getElementsByClassName("el-message-box")[0].style.width = "520px"
    },
    addDataConfigList() {
      this.dataConfigList.push({});
    }, deleteDataConfig(index) {
      this.dataConfigList.splice(index, 1)
    }
  }
  ,
}
;
</script>
